#include<stdio.h>

#define BUFFSIZE 32

int myreversal (FILE *rfp, FILE *wfp)
{
	char ch;
	long int size = 0;
	//定位到源文件流的末尾
	fseek (rfp, 0, SEEK_END);
	size = ftell(rfp);

	//定位到目标文件的文件流开头
	rewind (wfp);
	for(int i = size; i >= 0; i--)
	{
		fseek (rfp, i, SEEK_SET);
		ch = fgetc(rfp);
		if(ch == EOF){
			if(ferror (rfp)){
				return -1;
			}
		}
		fputc(ch,wfp);
	}
	return 0;
}
int main(int argc , char **argv){
	FILE *fp1, *fp2;
	char buf[BUFFSIZE] = {0};
	if( argc < 3 )
	{
		return -1;
	}
	fp1 = fopen(argv[1],"r");
	if(NULL == fp1)
	{
		fprintf(stderr,"fopen() failed!\n");
		return -1;
	}
	fp2 = fopen(argv[2],"w");
	if(NULL == fp2)
	{
		fprintf(stderr,"fopen() failed!\n");
		return -1;
	}
	myreversal(fp1,fp2);
/*	printf ("copy后的文件位置：%ld\n", ftell(fp1));

	//设置文件流到文件的开头位置
	rewind(fp1);
	printf("rewind 后：%ld\n", ftell(fp1));

	//定位文件流
	fseek(fp1, 5L, SEEK_SET);
	fgets (buf, BUFFSIZE, fp1);
	puts (buf);
*/
	fclose(fp1);
	fclose(fp2);
	return 0;
}
